package pl.gladiator.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import pl.gladiator.dao.UserEntityManagerImp;
import pl.gladiator.model.User;

@Controller
@RequestMapping("/users")
public class UserController {

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView addUser(User user, BindingResult errors,
			HttpServletRequest request, HttpServletResponse response) {
		UserEntityManagerImp uM = new UserEntityManagerImp();
		uM.save(user);
		ModelMap map = new ModelMap();

		return new ModelAndView("start", map);
	}

	@RequestMapping(value = "/register")
	public ModelAndView goToRegister() {
		ModelMap map = new ModelMap();
		User user = new User();
		map.put("user", user);
		return new ModelAndView("register", map);
	}

	@RequestMapping(value = "/notActivatedUsers")
	public ModelAndView notActivated() {
		ModelMap map = new ModelMap();
		UserEntityManagerImp uM = new UserEntityManagerImp();
		List<User> userList = uM.getNotActivatedUsers();
		map.put("userList", userList);
		return new ModelAndView("notActivatedUsers", map);
	}

	@RequestMapping(value = "/activate/{userPk}")
	public ModelAndView activate(@PathVariable String userPk) {
		ModelMap map = new ModelMap();
		UserEntityManagerImp uM = new UserEntityManagerImp();
		uM.update(Integer.parseInt(userPk), "TAK");
		List<User> userList = uM.getNotActivatedUsers();
		map.put("userList", userList);
		return new ModelAndView("notActivatedUsers", map);
	}

	@RequestMapping(value = "/activatedUsers")
	public ModelAndView activated() {
		ModelMap map = new ModelMap();
		UserEntityManagerImp uM = new UserEntityManagerImp();
		List<User> userList = uM.getActivatedUsers();
		map.put("userList", userList);
		return new ModelAndView("activatedUsers", map);
	}

	@RequestMapping(value = "/deactivate/{userPk}")
	public ModelAndView deactivate(@PathVariable String userPk) {
		ModelMap map = new ModelMap();
		UserEntityManagerImp uM = new UserEntityManagerImp();
		uM.update(Integer.parseInt(userPk), "NIE");
		List<User> userList = uM.getNotActivatedUsers();
		map.put("userList", userList);
		return new ModelAndView("activatedUsers", map);
	}

	@RequestMapping(value = "/players")
	public ModelAndView players() {
		ModelMap map = new ModelMap();
		UserEntityManagerImp uM = new UserEntityManagerImp();
		List<User> userList = uM.getActivatedUsers();
		map.put("userList", userList);
		return new ModelAndView("players", map);
	}

	@RequestMapping(value = "/playerDetails/{userPk}")
	public ModelAndView playerDetails(@PathVariable String userPk) {
		ModelMap map = new ModelMap();
		UserEntityManagerImp uM = new UserEntityManagerImp();
		User user = uM.findUserByPk(Integer.parseInt(userPk));
		map.put("user", user);
		return new ModelAndView("playerDetails", map);
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(User user, BindingResult errors,
			HttpServletRequest request, HttpServletResponse response) {
		ModelMap map = new ModelMap();
		UserEntityManagerImp uM = new UserEntityManagerImp();
		User userDb = uM.checkLogin(user);
		if (userDb != null) {
			HttpSession session = request.getSession();
			session.setAttribute("user", userDb);
		}
		System.out.println("Controller: " + userDb);
		return new ModelAndView("start", map);
	}
	
	@RequestMapping(value="/logout")
	 public ModelAndView logut(HttpServletRequest request){
		ModelMap map = new ModelMap();
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		User user = new User();
		map.put("user", user);
		return new ModelAndView("start", map);
}

}
